class Solution {
public:
    int countPairs(vector<int>& a, int k) {
        //o(nlogn)
        sort(a.begin(), a.end());
        int ans = 0;
        int n = a.size();
        for (auto x : a) {
            cout << x << " ";
        }
        cout << endl;
        for (int i = 0; i < n; i++) {
            int x = k - a[i];
            int l = -1, r = n;
            while (l + 1 < r) {
                int mid = l + r >> 1;
                if (a[mid] < x)l = mid;
                else r = mid;
            }
            cout << "x==" << a[i] << endl;
            cout << "l==" << l << endl;
            if (l > i) ans += l - i;
        }
        return ans;
    }
};